package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.CollectionUtils
import com.blankj.utilcode.util.SizeUtils
import com.lv.common.imp.OnPositionTListener
import com.lv.common.utils.showToast
import com.lv.service.R
import com.lv.service.adapter.TextTabSelectedAdapter
import com.lv.service.bean.CommonTab
import com.lxj.xpopup.core.BottomPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class ShowProvinceSelectedBottomPopupView(
    private val mContext: Context,
    private var mIsOpen: Boolean = false,
    private val mAllCategory: List<CommonTab>? = null,
    private val selectedCategorys: List<CommonTab>? = null,
    private val showMore: Boolean = false,
    private var onPopSureListener: OnPositionTListener<List<CommonTab>>?
) :
    BottomPopupView(mContext) {
    private val MAXA_SELECTED = 1000
    private var maxSelected = 1
    private var mPosition = -1
    private val name = ""
    private var rvContent: RecyclerView? = null
    private var viewTop: View? = null
    private var viewBottom: View? = null
    private var vSumbit: View? = null
    private var tvSubmit: TextView? = null
    private var llMoreCategory: LinearLayout? = null
    private val mCategorys = mutableListOf<CommonTab>()
    private val mSelectedCategorys = mutableListOf<CommonTab>()
    private var mOnPopSwitchListener: OnPositionTListener<Int>? = null

    private var mAdapter: TextTabSelectedAdapter? = null
    override fun getImplLayoutId(): Int {
        return R.layout.address_tab_bottom_popup
    }

    override fun onCreate() {
        super.onCreate()
        rvContent = findViewById<RecyclerView>(R.id.rv_content)
        viewTop = findViewById<View>(R.id.view_top)
        viewBottom = findViewById<View>(R.id.view_bottom)
        vSumbit = findViewById<View>(R.id.v_sumbit)
        tvSubmit = findViewById<TextView>(R.id.tv_submit)
        llMoreCategory = findViewById<LinearLayout>(R.id.ll_more_category)
        findViewById<ImageView>(R.id.iv_close).setOnClickListener { dismiss() }
        llMoreCategory?.visibility = if (showMore) View.VISIBLE else View.GONE
        llMoreCategory?.setOnClickListener {
            mOnPopSwitchListener?.onClick(0, 0)
        }

        initView()
        initEvnet()
    }

    override fun onShow() {
        super.onShow()
        initData()

    }

    private fun initView() {
        initAdapter()
        initData()
        initEnable()
    }

    private fun initAdapter() {
        val gridLayoutManager = GridLayoutManager(context, 3)
        rvContent?.layoutManager = gridLayoutManager
        mAdapter = TextTabSelectedAdapter(mCategorys)
        rvContent?.adapter = mAdapter
    }

    private fun initData() {

        maxSelected = if (mIsOpen) MAXA_SELECTED else 1
        mAllCategory?.let {
            mCategorys.clear()
            mCategorys.addAll(mAllCategory)
        }
        mSelectedCategorys.clear()
        selectedCategorys?.let {
            mSelectedCategorys.addAll(selectedCategorys)
        }
        for (i in 0 until mCategorys.size) {
            val id = mCategorys[i].name ?: ""
            mCategorys[i].isSelcted = false
            for (j in 0 until mSelectedCategorys.size) {
                val id1 = mSelectedCategorys[j].name ?: ""
                if (id.contains(id1) || id1.contains(id)) {
                    mPosition = i
                    mCategorys[i].isSelcted = true
                    break
                }
            }
        }
        mAdapter?.notifyDataSetChanged()

    }

    private fun initEnable() {
        if (CollectionUtils.isNotEmpty(mSelectedCategorys)) {
            tvSubmit?.alpha = 1.0f
            vSumbit?.isClickable = true
        } else {
            tvSubmit?.alpha = 0.6f
            vSumbit?.isClickable = false
        }
    }

    private fun initEvnet() {

        viewTop?.setOnClickListener {
            dismiss()
        }
        mAdapter?.onViewClickListener = OnPositionTListener { position, data ->
            val selected = !data.isSelcted
            if (selected) {
                if (maxSelected == 1) {
                    mSelectedCategorys.clear()
                    if (mPosition == -1) {
                        mCategorys[position].isSelcted = selected
                        mAdapter?.notifyItemChanged(position)
                        mSelectedCategorys.add(data)
                    } else {
                        mCategorys[mPosition].isSelcted = !selected
                        mAdapter?.notifyItemChanged(mPosition)
                        mSelectedCategorys.remove(mCategorys[mPosition])
                        mSelectedCategorys.add(data)
                        mCategorys[position].isSelcted = selected
                        mAdapter?.notifyItemChanged(position)
                    }
                    mPosition = position
                } else {
                    if (mSelectedCategorys.size < maxSelected) {
                        mCategorys[position].isSelcted = selected
                        mAdapter?.notifyItemChanged(position)
                        mSelectedCategorys.add(data)
                        mPosition = position
                    } else {
                        showToast("最多只能选择${maxSelected}个分类")
                    }
                }

            } else {
                mCategorys[position].isSelcted = selected
                mAdapter?.notifyItemChanged(position)
                for (k in 0 until mSelectedCategorys.size) {
                    val telSecondCategory = mSelectedCategorys[k]
                    if (data.name == telSecondCategory.name) {
                        mSelectedCategorys.removeAt(k)
                        break
                    }
                }
                if (mSelectedCategorys.size == 0) {
                    mPosition = -1
                }
            }
            initEnable()
        }
        vSumbit?.setOnClickListener {
            onPopSureListener?.onClick(0, mSelectedCategorys)
            dismiss()
        }

    }

    private fun setDismissDialog() {
        dismiss()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()


    }


    fun setOnSwitchListener(onPopSwitchListener: OnPositionTListener<Int>?) {
        this.mOnPopSwitchListener = onPopSwitchListener
    }

    fun setOpen(isOpen: Boolean) {
        mIsOpen = isOpen
        maxSelected = if (mIsOpen) MAXA_SELECTED else 1
//        var selected = -1
        if (!mIsOpen) {
            for (i in 0 until mCategorys.size) {
                val TelSecondCategory = mCategorys[i]
                if (TelSecondCategory.isSelcted) {
                    mCategorys[i].isSelcted = false
                    mAdapter?.notifyItemChanged(i)
                }
            }
            if (mSelectedCategorys.isNotEmpty()) {
                val TelSecondCategory1 = mSelectedCategorys[0]
                mPosition = -1
                for (i in 0 until mCategorys.size) {
                    if (TelSecondCategory1.name == mCategorys[i].name) {
                        mPosition = i
                        mCategorys[mPosition].isSelcted = true
                        mAdapter?.notifyItemChanged(mPosition)
                    }
                }

                mSelectedCategorys.clear()
                mSelectedCategorys.add(TelSecondCategory1)
            }
//            for (i in 0 until mCategorys.size) {
//                val commontab = mCategorys[i]
//                if (commontab.isSelcted) {
//                    if (selected == -1) {
//                        selected = i
//                    } else {
//                        mCategorys[i].isSelcted = false
//                        mAdapter?.notifyItemChanged(i)
//                    }
//                }
//            }
//            if (selected != -1) {
//                mPosition = selected
//            }
        }
//        val sele = mCategorys.filter { item -> item.isSelcted }
//        mSelectedCategorys.clear()
//        if (CollectionUtils.isNotEmpty(sele)) {
//            mSelectedCategorys.addAll(sele)
//        }
        onPopSureListener?.onClick(0, mSelectedCategorys)
        initEnable()
    }
}